from django.db import models
from django.contrib.auth.models import User
from eventapp.models import Event
    
class TimelineCategory(models.Model):
    name = models.CharField(max_length=50);
    
    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name
    
    class Meta:
        db_table = 'Categories'
        
    
class Timeline(models.Model):
    title = models.CharField(max_length=100) 
    description = models.TextField(null=True)
    creator = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
    categories = models.ManyToManyField(TimelineCategory, 
                                                 db_table="TimelineCategories")
    initial_date = models.DateTimeField(null=True)
    final_date = models.DateTimeField(null=True)
    creation_date = models.DateTimeField()
    privacy = models.IntegerField()
    cover = models.ForeignKey(Event, related_name="cover", null=True)
        
    events = models.ManyToManyField(Event, db_table="TimelineEvents")

    def getValue(self, key):
        if key == "headline" or key == "title":
            return self.title
        elif key == "description":
            return self.description
        else:
            return None

    def toDict(self, values):
        timeline = {}
        
        for i in values:
            if self.getValue(i) != None:
                timeline[i] = self.getValue(i)
                
        return timeline

    def __unicode__(self):
        return self.title
    
    def __str__(self):
        return self.title

    class Meta:
        db_table = 'Timelines'